﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using UtilsORM;
using UtilsORM.MySqlData;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class DeviceValueCurrentInterval : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();
        // GET: Mager_Department
        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_product product)
        {
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            string sqlwhere = "  ";

            //try
            //{



            string sqlDevice = "select *,gwd.d_code gatewaycode, (case when gw_live_time>date_add('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',interval -2 minute) then '在线' when gw_live_time is not null then '网关离线' when  ls_live_state_set_time>date_add('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',interval -2 minute) then '设备在线' else '设备离线' end) as state from iot_device d  left join iot_device_gateway on d_gw_d_guid=gw_d_guid inner join iot_device_model on d_model_guid=m_guid  inner join iot_device_model_type on m_type_guid=t_guid left join iot_device gwd on gwd.d_guid=gw_d_guid left join iot_device_live_state on ls_d_guid=d.d_guid where d.d_p_guid='" + product.p_guid + "'  and d.d_status=0   and t_code in(1001,1004,1007,1006) order by d.d_code";
            DataTable dtDevice = MySqlYihongIot.GetDataTable(sqlDevice);
            ViewData["dtDevice"] = dtDevice;

            string sqlvalue = "select c.*,v_unit from iot_device   inner join iot_device_gateway on d_gw_d_guid=gw_d_guid inner join iot_device_model on d_model_guid=m_guid  inner join iot_device_model_type on m_type_guid=t_guid inner join iot_device_value_current c on vc_d_guid=d_guid inner join iot_device_model_variable_relation  on mv_model_guid=d_model_guid  inner join iot_device_model_variable on  mv_v_guid=v_guid   and v_key=vc_key where  d_p_guid='" + product.p_guid + "'  and d_status=0   and  t_code in(1001,1004,1007,1006) order by c.vc_id asc";
            DataTable dtvalue = MySqlYihongIot.GetDataTable(sqlvalue);
            ViewData["dtvalue"] = dtvalue;


            string getewaysql = "select *,(case when gw_local_ip is null or gw_local_ip='' then '待连网' else gw_local_ip end) ip,case when ls_live_state_set_time>date_add('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "',interval -2 minute) then '主机在线' else '主机离线' end onlinestate " +
                            "   from   iot_device_gateway a join iot_device d on d.d_guid=gw_d_guid  left join iot_device_live_state on ls_d_guid=d_guid   " +
                            " where d.d_status=0 and d.d_p_guid='" + product.p_guid + "'  order by a.gw_id asc";
            DataTable getewaydt = MySqlYihongIot.GetDataTable(getewaysql);
            ViewData["geteway_vd"] = getewaydt;
            //}
            //catch (Exception ex)
            //{
            //    ViewData["Device_info"] = new DataTable();
            //    ViewData["DeviceTemperature_info"] = new DataTable();
            //}
            return View();
        }



    }
}